package com.wulab.backend.controller;

import com.wulab.backend.dto.ApiResponse;
import com.wulab.backend.entity.Dataset;
import com.wulab.backend.service.DatasetService;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

@RestController
@RequestMapping("/api/dataset")
public class DatasetController {
    private final DatasetService datasetService;

    public DatasetController(DatasetService datasetService) {
        this.datasetService = datasetService;
    }

    @PostMapping("/import")
    public ApiResponse<String> importExcel(@RequestParam("file") MultipartFile file) {
        try {
            // 处理文件并导入数据
            datasetService.importDataset(file);
            // 返回成功的 JSON 响应
            return new ApiResponse<>(true, "File uploaded and data imported successfully!", null);
        } catch (Exception e) {
            e.printStackTrace();
            // 返回失败的 JSON 响应
            return new ApiResponse<>(false, "Failed to import data from file: " + e.getMessage(), null);
        }
    }

    @GetMapping("/get_all_datasets")
    public ApiResponse<List<Dataset>> getAllUsers() {
        List<Dataset> datasets = datasetService.getDatasets();
        return new ApiResponse<>(true, "Data fetched successfully", datasets);
    }
}
